package com.ganee.agora.view

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.ganee.agora.R
import com.ganee.agora.adapter.SearchListAdapter
import com.ganee.agora.model.SearchListEntity
import com.ganee.agora.viewmodel.SearchViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.toolbar_with_icon.*

@Route(path = Constance.ACTIVITY_URL_SEARCH)
class SearchActivity : BaseActivity<SearchViewModel>(), View.OnClickListener {

    val dataList:MutableList<SearchListEntity> = mutableListOf()
    private var mAdapter:SearchListAdapter? = null

    override fun getViewModel(): SearchViewModel {
        return ViewModelProviders.of(this@SearchActivity).get(SearchViewModel::class.java)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)
        ARouter.getInstance().inject(this)
        initView()
        initData()
        initAdapter()
    }



    private fun initAdapter() {
        categoryRecyclerView.setHasFixedSize(true)
        categoryRecyclerView.layoutManager = LinearLayoutManager(this)

        mAdapter = SearchListAdapter(dataList)
        categoryRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {

            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val searchListEntity = adapter.getItem(position) as SearchListEntity
                ARouter
                        .getInstance()
                        .build(Constance.ACTIVITY_URL_SEARCH_FILTER)
                        .withString("activeType",searchListEntity.title)
                        .navigation()
            }

        })
        categoryRecyclerView.adapter = mAdapter
    }

    private fun initData() {
        dataList.add(SearchListEntity("Music",R.drawable.violin))
        dataList.add(SearchListEntity("Drama",R.drawable.drama))
        dataList.add(SearchListEntity("Dance",R.drawable.dance))
        dataList.add(SearchListEntity("Films",R.drawable.download))
        dataList.add(SearchListEntity("Animals",R.drawable.animals))
        dataList.add(SearchListEntity("Exercise",R.drawable.sports))
        dataList.add(SearchListEntity("Yoga",R.drawable.yoga))
        dataList.add(SearchListEntity("Meditation",R.drawable.meditation))
        dataList.add(SearchListEntity("Nature",R.drawable.nature))
        dataList.add(SearchListEntity("Food",R.drawable.food))
        dataList.add(SearchListEntity("Cultural events",R.drawable.globe))
        dataList.add(SearchListEntity("Peer support",R.drawable.peer_support))
        dataList.add(SearchListEntity("Spirituality",R.drawable.pray))
        dataList.add(SearchListEntity("Visual arts",R.drawable.painting))
        dataList.add(SearchListEntity("Museums",R.drawable.museum))
    }


    private fun initView() {
        toolbarCancel.setOnClickListener(this)
        toolbarRight.visibility = View.INVISIBLE
        search_ll.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.toolbarCancel -> {
                finish()
            }
            R.id.search_ll -> {
                ARouter
                        .getInstance()
                        .build(Constance.ACTIVITY_URL_SEARCH_FILTER)
                        .withString("activeType","")
                        .navigation()
            }
        }
    }
}
